package tagcloud.gui;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class FileChooserPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JFileChooser fileChooser = null;
	private JLabel descriptionLabel = null;
	private boolean selectionMade = false;
	private File selectedFile = null;

	/**
	 * This is the default constructor
	 */
	public FileChooserPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 0;
		gridBagConstraints1.weightx = 1.0D;
		gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints1.insets = new Insets(8, 8, 0, 8);
		gridBagConstraints1.gridy = 0;
		descriptionLabel = new JLabel();
		descriptionLabel.setText("Choose the document you which to upload for analysis:");
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.weightx = 1.0D;
		gridBagConstraints.weighty = 1.0D;
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		gridBagConstraints.gridy = 1;
		this.setSize(640, 480);
		this.setLayout(new GridBagLayout());
		this.add(getFileChooser(), gridBagConstraints);
		this.add(descriptionLabel, gridBagConstraints1);
	}

	/**
	 * This method initializes fileChooser	
	 * 	
	 * @return javax.swing.JFileChooser	
	 */
	private JFileChooser getFileChooser() {
		if (fileChooser == null) {
			fileChooser = new JFileChooser();
			fileChooser.setMultiSelectionEnabled(false);
			fileChooser.addActionListener(this);
		}
		return fileChooser;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == JFileChooser.APPROVE_SELECTION) {
			selectedFile = getFileChooser().getSelectedFile();
			selectionMade = true;
		} else if(e.getActionCommand() == JFileChooser.CANCEL_SELECTION) {
			selectedFile = null;
			selectionMade = true;
		}
	}
	
	protected void reset() {
		selectionMade = false;
		selectedFile = null;
	}
	
	protected boolean selectionHasBeenMade() {
		return selectionMade;
	}
	
	protected File selectedFile() {
		return selectedFile;
	}

}
